#ifndef TENTACLE_H
#define TENTACLE_H

#include "math3d.h"
#include "utils.h"

#include <sys/types.h>

///////////////////////////////////////////////////////////////////////////////

class Tentacle
{
  struct Vertex
  {
    vec3 pos;
    vec3 normal;
    vec2 st;
    int8_t boneids[4];
    vec4 boneblends;
  };
  struct Triangle { unsigned short v[3]; };

  static const uint SIDES = 12;
  static const uint MAX_BONES = 60; // remember to also update vertex shader

public:
  Tentacle(uint boneCount, uint segmentCount, float radius, float height, GLuint texture);
  ~Tentacle();

  void draw(uint64_t utime, const mat4 &projMat, const mat4 &camMatInv, const mat4 &modelMat);

  static void compileShaders();
  static void releaseShaders();


private:
  void buildRestSkeleton();
  void createMesh();

private:
  static GLuint s_program;

  static GLint s_posAttrib;
  static GLint s_normalAttrib;
  static GLint s_stAttrib;
  static GLint s_bonesAttrib;
  static GLint s_blendsAttrib;

  static GLint s_lightDiffuseUniform;
  static GLint s_lightSpecularUniform;
  static GLint s_lightPositionUniform;
  static GLint s_lightKUniform;

  static GLint s_mvmatUniform;
  static GLint s_bonesUniform;

  static GLint s_textureSampler;

  GLuint m_texture;

  uint m_boneCount;
  uint m_segmentCount;
  float m_radius;
  float m_height;

  GLuint m_vertexBuffer;
  GLuint m_indexBuffer;

  struct
  {
    mat4 restMat;
    mat4 restMatInverted;
  } m_bones[MAX_BONES];
};

///////////////////////////////////////////////////////////////////////////////

#endif
